<?php

namespace App\Http\Controllers\Cruise\FeishuMini;

use App\Http\Controllers\BaseController;
use App\Models\Cruise\CruiseCheckDetailModel;
use App\Models\Cruise\CruiseOrderModel;
use App\Models\Cruise\CruiseOrderProcessModel;
use App\Service\Cruise\Mission\CheckInputService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class CheckInputController extends BaseController
{
    public function info(Request $request, int $cruiseOrderId): JsonResponse
    {
        $userId = getRmsUserId();

        $data = (new CheckInputService($cruiseOrderId, $userId))->getInputObjectFill();

        return $this->success($data);
    }

    public function update(Request $request, int $cruiseOrderId)
    {
        $field      = (string)$request->get('field');
        $value      = $request->get('value');
        $auditValue = (array)$request->get('auditValue');
        $userId     = getRmsUserId();

        $this->validate($request, [
            'field'      => 'required|string',
//            'value'      => 'required',  // 得兼容穿空字符串过来的情况
            'auditValue' => 'array',
        ]);

        (new CheckInputService($cruiseOrderId, $userId))->saveUserContentJson($field, $value, $auditValue);

        return $this->success();
    }

    public function submit(Request $request, int $cruiseOrderId)
    {
        $userId = getRmsUserId();

        (new CheckInputService($cruiseOrderId, $userId))->submitOrder();

        return $this->success();
    }

    public function audit(Request $request, int $cruiseOrderId)
    {
        $auditStatus  = $request->get('auditStatus', CruiseOrderProcessModel::STATUS_OK);
        $auditContent = $request->get('auditContent');
        $veto         = $request->get('veto');
        $qualityScore = $request->get('qualityScore');
        $userInfo     = getRmsUserInfoByAuth();
        $userId       = $userInfo->id;

        $cruiseOrderModel = CruiseOrderModel::query()->find($cruiseOrderId);
        if ($cruiseOrderModel) {
            if ($cruiseOrderModel->status == CruiseOrderModel::STATUS_NOT_REVIEW) {

                // 总部
                $this->validate($request, [
                    'auditContent' => 'required|string',
                    'veto'         => [
                        'required',
                        'integer',
                        Rule::in([CruiseCheckDetailModel::VETO_ON, CruiseCheckDetailModel::VETO_OFF])
                    ],
                    'qualityScore' => 'required|integer|min:0',
                ]);

            } elseif ($cruiseOrderModel->status == CruiseOrderModel::STATUS_WAIT_MODIFY_EXT) {

                // 三方
                $this->validate($request, [
                    'auditStatus'  => [
                        'required',
                        'integer',
                        Rule::in([CruiseOrderProcessModel::STATUS_OFF, CruiseOrderProcessModel::STATUS_OK])
                    ],
                    'auditContent' => 'required|string',
                ]);
            }
        }

        $data = [
            'auditStatus'  => $auditStatus,
            'auditContent' => $auditContent,
            'veto'         => $veto,
            'qualityScore' => $qualityScore,
        ];

        (new CheckInputService($cruiseOrderId, $userId))->audit($data);

        return $this->success();
    }
}
